#!/bin/bash
# Author: fanyongfeng
# 0 1 * * * /bin/bash /root/script/cassandra_data_backup.sh &> /backup/cassandra_data_backup/backup.log

TODAY=$(date +%Y%m%d)
DATA_DIR="/data/cassandra-data/data"
BACKUP_DIR="/backup/cassandra_data_backup"

mkdir -p ${BACKUP_DIR}/${TODAY}

source /etc/profile
${CASSANDRA_HOME}/bin/nodetool clearsnapshot
${CASSANDRA_HOME}/bin/nodetool snapshot -t ${TODAY}
[[ $? -ne 0 ]] && echo "${TODAY} 快照备份失败" && exit 1

snapshots_dir_list="$(find ${DATA_DIR} -type d -name 'snapshots' | sed "s#${DATA_DIR}/##g" | sed 's#/snapshots##g')"
for snap_dir in ${snapshots_dir_list}
do
  mkdir -p ${BACKUP_DIR}/${TODAY}/${snap_dir}
  cp -rp ${DATA_DIR}/${snap_dir}/snapshots ${BACKUP_DIR}/${TODAY}/${snap_dir}
done

find ${BACKUP_DIR} -type d -mtime +30 | xargs -r -n 10 rm -rf